package com.cskt.petproject178.controller;

import com.cskt.petproject178.pojo.Authened;
import com.cskt.petproject178.pojo.entity.User;
import com.cskt.petproject178.pojo.vo.AddressVO;
import com.cskt.petproject178.pojo.vo.ResultVO;
import com.cskt.petproject178.pojo.vo.StatusEnum;
import com.cskt.petproject178.pojo.vo.UserInfoVO;
import com.cskt.petproject178.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 27013
 **/

@RestController
@RequestMapping(value = "/ums")
public class UserController {
    private static  final Logger log= LoggerFactory.getLogger(UserController.class);
    @Resource
    private UserService userService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private ObjectMapper objectMapper;


    @PostMapping(value = "/sms/sendSms")
    public ResultVO<?> sendSms(@RequestParam String phone, @RequestParam Integer codeType){
        Boolean result = userService.sendCode(phone, codeType);
        return  result? ResultVO.OK(): ResultVO.ERR();
    }



    /**
     * 登录 ?不确定泛型
     * @param phone
     * @param sms
     * @return
     */
    @PostMapping(value = "/user/login")
    public ResultVO<?> login(@RequestParam String phone, @RequestParam String sms){
        String token = userService.login(phone, sms);
        if (StringUtils.hasText(token)) {
            Map<String,String> map=new HashMap<>();
            map.put("token", token);
            return ResultVO.OK(map);
        }
        return ResultVO.ERR();
    }

    @GetMapping(value = "/user/getUserByToken")
    @Authened
    public ResultVO<?> getUsreByToken(@RequestHeader(value = "Token") String token){
      String tokenKey="pet:token:"+token;
      String userJson=stringRedisTemplate.opsForValue().get(tokenKey);
        try {
            //将json字符串转换为user对象
            UserInfoVO   userInfoVO = objectMapper.readValue(userJson, UserInfoVO.class);
            return  ResultVO.OK(userInfoVO);
        } catch (JsonProcessingException e) {
            log.error("获取当前登录用户信息反序列化异常",e);
            return ResultVO.ERR();
        }
    }
    @GetMapping("/user/logout")
    public ResultVO<Void> loginOut(@RequestHeader(value = "Token") String Token){
        String token="pet:token:"+Token;
        //2.删除当前请求的token
         stringRedisTemplate.delete(token);
        return  ResultVO.OK();
    }

    @GetMapping(value = "/receiverAddress/list")
    @Authened
    public ResultVO<List<AddressVO>> selectAddressVOByUserId(@RequestParam Long userId){
        return ResultVO.OK(userService.selectAddressVOByUserId(userId));
    }
}
